﻿using LdtDataEngin.ModuleDetail.ParagraphAnalysis;
using LdtDataEngin.ModuleDetail.RegCommon;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.DataModel.DynamicData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.AnalysisModule.ParagraphAnalysis
{
    public class ParagraphLocation
    {
        /// <summary>
        /// 中间数据层
        /// </summary>
        public DataContext dataContext;

        /// <summary>
        /// 执行段落定位提取
        /// </summary>

        public void Extrat(List<ConfigStateModel> configModels,string modelName)
        {
            if (dataContext.DataLayerAns.Duanluos == null)
            {
                dataContext.DataLayerAns.Duanluos = new List<An_duanluo>();
            }
            ProcessAnalysis processAnalysis = new ProcessAnalysis(dataContext, modelName);
            Dictionary<string, string> dicOrg = new Dictionary<string, string>();
            string originalText = "";
            foreach (ConfigStateModel configStateModel in configModels)
            {
                if (configStateModel.number.ToString().IndexOf(".") < 0)
                {   //根节点
                    if (string.IsNullOrEmpty(originalText))
                    {
                        originalText = dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong;

                    }
                    List<string> valueList = new List<string>();
                    valueList.Add(originalText);
                    InteractiveModel interactiveModel = processAnalysis.AnalysisProcessData(configStateModel.code, modelName, 1, valueList);
                    if (interactiveModel != null)
                    {
                        if (interactiveModel.ModelList.Count>0)
                        {
                            An_duanluo duanluo = new An_duanluo();
                            duanluo.Duanluoid = Snowflake.Instance().GetId().ToString();
                            duanluo.Duanluoneirong = interactiveModel.ModelList[0].Trim();
                            duanluo.Duanluobianhao = configStateModel.code.ToLower();
                            duanluo.DuanluoxuhaoDesc = configStateModel.number;
                            duanluo.Duanluomingcheng = configStateModel.zh_cn;
                            duanluo.Duanluoyuanwen = originalText;
                            dataContext.DataLayerAns.Duanluos.Add(duanluo);
                            dataContext.InteractiveShow.LogShow("段落名称：" + configStateModel.executecode + " 段落内容：" + interactiveModel.ModelList[0]);
                            originalText = originalText.Replace(interactiveModel.ModelList[0], "");
                            dicOrg.Add(configStateModel.number, interactiveModel.ModelList[0]);
                        }

                    }
                }
                else
                {
                    //子集节点
                    string[] numArry = configStateModel.number.Split('.');
                    var length = numArry.Length;
                    string parentNo = string.Join(".", numArry[..(length - 1)]);
                    if (dicOrg.ContainsKey(parentNo))
                    {
                        string org = dicOrg[parentNo];
                        List<string> valueList = new List<string>();
                        valueList.Add(org);
                        InteractiveModel interactiveModel = processAnalysis.AnalysisProcessData(configStateModel.code, modelName, 1, valueList);
                        if (interactiveModel!=null)
                        {
                            An_duanluo an_Duanluo = dataContext.DataLayerAns.Duanluos.FirstOrDefault(a => a.DuanluoxuhaoDesc == parentNo);
                            if (interactiveModel.ModelList.Count>0)
                            {
                                An_duanluo duanluo = new An_duanluo();
                                duanluo.Duanluoid = Snowflake.Instance().GetId().ToString();
                                duanluo.Duanluoneirong = interactiveModel.ModelList[0].Trim();
                                duanluo.Duanluobianhao = configStateModel.code.ToLower();
                                duanluo.DuanluoxuhaoDesc = configStateModel.number;
                                duanluo.Duanluofujiid = an_Duanluo.Duanluoid;
                                duanluo.Duanluomingcheng = configStateModel.zh_cn;
                                duanluo.Duanluoyuanwen = org;
                                dataContext.DataLayerAns.Duanluos.Add(duanluo);
                                dicOrg.Add(configStateModel.number, interactiveModel.ModelList[0]);
                                dataContext.InteractiveShow.LogShow("段落名称：" + configStateModel.code + " 段落内容：" + interactiveModel.ModelList[0]);
                                
                                org = org.Replace(interactiveModel.ModelList[0], "");
                                dicOrg[parentNo] = org;
                            }
                        }
                    }
                }
            }
        
        }
    }
}
